//
//  QuDanSkillCompelteController.m
//  QuDanOEM
//
//  Created by xiaohuang on 2021/11/7.
#import "QuDanSkillCompelteController.h"
#import "UIButton+Gradient.h"
#import "QuDanCompleteSkillViewCell.h"
#import "QuDanCompleteMoneyViewCell.h"
#import "QuDanCompleteAgeViewCell.h"
#import "QuDanCompleteSexViewCell.h"
#import "QuDanCompleteTimeViewCell.h"
#import "QuDanCompleteTimeDetailViewCell.h"
#import "QuDanSendCenterNetWork.h"
#import "QuDanCategoryModel.h"
#import "BRStringPickerView.h"
#import "BRDatePickerView.h"
#import "QuDanOrderTimeModel.h"
#import "DLPickerView.h"
@interface QuDanSkillCompelteController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UIButton *nextBtn;
@property (nonatomic,strong)UICollectionView *completeCollectionView;
@property (nonatomic,strong)NSMutableArray *timeDetailArr;
@property (nonatomic,strong)NSMutableArray *timeDetailContentArr;
@property (nonatomic,strong)NSMutableArray *skillListArr;
@property (nonatomic,strong)NSMutableArray *skillNameArr;
@property (nonatomic,strong)NSMutableArray *weekListArr;
@property (nonatomic,strong)QuDanCompleteSkillViewCell *skillCell;
@property (nonatomic,strong)QuDanCompleteTimeDetailViewCell *detailCell;
@property (nonatomic,strong)QuDanCompleteMoneyViewCell *moneyCell;
@property (nonatomic,strong)NSDictionary *cellIdentifyDic;
@property (nonatomic,copy) NSString *ageLimitStr;
@property (nonatomic,copy) NSString *sexLimtStr;
@property (nonatomic,copy) NSString *uitStr;
@property (nonatomic,strong)NSArray *leftArr;
@end
@implementation QuDanSkillCompelteController
- (NSMutableArray *)weekListArr
{
    if (!_weekListArr) {
        _weekListArr = [NSMutableArray arrayWithObjects:@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",@"周日", nil];
    }
    return _weekListArr;
}

- (NSMutableArray *)timeDetailContentArr
{
    if (!_timeDetailContentArr) {
        _timeDetailContentArr = [NSMutableArray array];
    }
    return _timeDetailContentArr;
}

- (NSMutableArray *)timeDetailArr
{
    if (!_timeDetailArr) {
        _timeDetailArr = [NSMutableArray array];
    }
    return _timeDetailArr;
}
- (NSMutableArray *)skillNameArr
{
    if (!_skillNameArr) {
        _skillNameArr = [NSMutableArray array];
    }
    return _skillNameArr;
}
- (NSMutableArray *)skillListArr
{
    if (!_skillListArr) {
        _skillListArr = [NSMutableArray array];
    }
    return _skillListArr;
}
- (UICollectionView *)completeCollectionView
{
    if (!_completeCollectionView) {
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
        flow.scrollDirection = UICollectionViewScrollDirectionVertical;
        flow.minimumLineSpacing = 0;//行间距
        flow.minimumInteritemSpacing = 0;//列间距
        _completeCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flow];
        _completeCollectionView.backgroundColor =  [UIColor clearColor];
        _completeCollectionView.showsVerticalScrollIndicator = NO;
        _completeCollectionView.showsHorizontalScrollIndicator = NO;
        _completeCollectionView.delegate = self;
        _completeCollectionView.dataSource = self;
        [_completeCollectionView registerNib:[UINib nibWithNibName:@"QuDanCompleteSkillViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanCompleteSkillViewCell"];
        [_completeCollectionView registerNib:[UINib nibWithNibName:@"QuDanCompleteMoneyViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanCompleteMoneyViewCell"];
        [_completeCollectionView registerNib:[UINib nibWithNibName:@"QuDanCompleteAgeViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanCompleteAgeViewCell"];
        [_completeCollectionView registerNib:[UINib nibWithNibName:@"QuDanCompleteSexViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanCompleteSexViewCell"];
        [_completeCollectionView registerNib:[UINib nibWithNibName:@"QuDanCompleteTimeViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanCompleteTimeViewCell"];
        //[_completeCollectionView registerNib:[UINib nibWithNibName:@"QuDanCompleteTimeDetailViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanCompleteTimeDetailViewCell"];
    }
    return _completeCollectionView;
}
- (UIButton *)nextBtn
{
  if (!_nextBtn) {
        _nextBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 70, 35)];
        _nextBtn.backgroundColor = QD_RGB(255, 81, 33);
        [_nextBtn setTitle:@"发布" forState:UIControlStateNormal];
        [_nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
             _nextBtn.layer.cornerRadius = 35/2;
        _nextBtn.layer.masksToBounds = YES;
        _nextBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_nextBtn addTarget:self action:@selector(actionupNext:) forControlEvents:UIControlEventTouchUpInside];
        _nextBtn.backgroundColor = [UIColor bm_colorGradientChangeWithSize:CGSizeMake(70, 35) direction:PageGradientChangeDirectionLevel startColor:PostColorP(0xFF5121,1) endColor:PostColorP(0xFFC021,1)];
      
    }
    return _nextBtn;
}
-(void)actionupNext:(UIButton *)btn
{
    NSLog(@"发布");
    if (QD_IS_EMPTY(self.skillCell.skillTF.text)) {
        [SVProgressHUD showInfoWithStatus:@"请选择技能"];
        return;
    }
    if (self.moneyCell.centerPriceLab.text <= 0) {
        [SVProgressHUD showInfoWithStatus:@"金额不能为0"];
        return;
    }
    if (self.timeDetailArr.count == 0) {
        [SVProgressHUD showInfoWithStatus:@"请添加接单时间"];
        return;
    }
    NSMutableString *timeLimitStr = [NSMutableString string];
    for (int i = 0; i< self.timeDetailArr.count; i++) {
        NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i inSection:5];
        QuDanCompleteTimeDetailViewCell *cell = (QuDanCompleteTimeDetailViewCell *)[self.completeCollectionView cellForItemAtIndexPath:indexpath];
        NSString *weekStr = [self getformatterStr:cell.typeLab.text];
        NSString *timeStr = cell.timeLab.text;
        NSLog(@"weekStr.text:%@--timeStr:text:%@",weekStr,timeStr);
        if (QD_IS_EMPTY(timeStr)) {
            [SVProgressHUD showInfoWithStatus:@"请选择接单具体时间"];
            return;
        }
        if (i == self.timeDetailArr.count-1) {
            [timeLimitStr appendString:weekStr];
            [timeLimitStr appendString:@"_"];
            [timeLimitStr appendString:timeStr];
        }else{
            [timeLimitStr appendString:weekStr];
            [timeLimitStr appendString:@"_"];
            [timeLimitStr appendString:timeStr];
            [timeLimitStr appendString:@","];
        }
    }
    NSLog(@"timeLimitStr:%@",timeLimitStr);
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    //param[@"areaLimit"] = @"";
    //param[@"cityCode"] = @"";
    //param[@"cityName"] = @"";
    //param[@"lat"] = @"";
    //param[@"lng"] = @"";
    param[@"ageLimit"] = self.ageLimitStr;
    param[@"categoryId"] = [self getCategoryIDWitheCategoryName:self.skillCell.skillTF.text];
    param[@"genderLimit"]  = self.sexLimtStr;
    if (QD_IS_EMPTY(self.pictureAddressStr)) {//图片为空了
    }else{
        param[@"imgs"] = self.pictureAddressStr;
    }
    param[@"MId"] = PostManage.shareInstance.userModel.ids;
    param[@"otherDesc"] = self.skillContentStr;
    param[@"price"]     = self.moneyCell.centerPriceLab.text;
    param[@"timeLimit"] = timeLimitStr;
    param[@"unit"] = @"0";
    NSLog(@"param:%@",param);
    [[QuDanSendCenterNetWork shareInstance]requestSendCenterJNNetWork:param viewController:self andBlock:^(id  _Nullable data, BOOL result) {
        NSLog(@"发布技能.data:%@",data);
        [SVProgressHUD showInfoWithStatus:@"发布成功"];
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
}
-(NSString *)getformatterStr:(NSString *)formatterStr
{
    NSString *weekStr = @"";
    if ([formatterStr isEqualToString:@"周一"]) {
        weekStr = @"1";
    }
    if ([formatterStr isEqualToString:@"周二"]) {
        weekStr = @"2";
    }
    if ([formatterStr isEqualToString:@"周三"]) {
        weekStr = @"3";
    }
    if ([formatterStr isEqualToString:@"周四"]) {
        weekStr = @"4";
    }
    if ([formatterStr isEqualToString:@"周五"]) {
        weekStr = @"5";
    }
    if ([formatterStr isEqualToString:@"周六"]) {
        weekStr = @"6";
    }
    if ([formatterStr isEqualToString:@"周日"]) {
        weekStr = @"7";
    }
    return weekStr;
}
-(NSString *)getCategoryIDWitheCategoryName:(NSString *)categoryName
{
    NSString *categotyID = @"";
    for (QuDanCategoryModel *model in self.skillListArr) {
        if ([model.caption isEqualToString:self.skillCell.skillTF.text]) {
            categotyID = [NSString stringWithFormat:@"%ld",(long)model.id];
            break;
        }
    }
    return categotyID;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = QD_RGB(247, 248, 252);
    self.navigationItem.title = @"完善信息";
    self.leftArr = @[@"00:00",@"01:00",@"02:00",@"03:00",@"04:00",@"05:00",@"06:00",@"07:00",@"08:00",@"09:00",@"10:00",@"11:00",@"12:00",@"13:00",@"14:00",@"15:00",@"16:00",@"17:00",@"18:00",@"19:00",@"20:00",@"21:00",@"22:00",@"23:00",@"24:00",];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.nextBtn];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"qd_back"] forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, 0, 40 , 40);
    btn.imageEdgeInsets = UIEdgeInsetsMake(0.0, -15.0, 0.0, 15.0);
    [btn addTarget:self action:@selector(onSendBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem = barItem;
    [self.view addSubview:self.completeCollectionView];
    [self.completeCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.offset(0);
    }];
    //默认值
    self.ageLimitStr = @"不限";
    self.sexLimtStr = @"不限";
}
-(void)onSendBtnAction:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -- QuDanSkillCompelteController 代理方法
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 6;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 5) {
        return self.timeDetailArr.count;
    }else{
        return 1;
    }
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
         self.skillCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanCompleteSkillViewCell" forIndexPath:indexPath];
        return self.skillCell;
    }else if(indexPath.section == 1){
        self.moneyCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanCompleteMoneyViewCell" forIndexPath:indexPath];
        return self.moneyCell;
    }else if(indexPath.section == 2){
        QuDanCompleteAgeViewCell *ageCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanCompleteAgeViewCell" forIndexPath:indexPath];
        [ageCell setQuDanCompleteAgeViewCellBlock:^(NSString * _Nonnull ageStr) {
            self.ageLimitStr = ageStr;
        }];
        return ageCell;
    }else if(indexPath.section == 3){
        QuDanCompleteSexViewCell *sexCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanCompleteSexViewCell" forIndexPath:indexPath];
        [sexCell setQuDanCompleteSexViewCellBlock:^(NSString * _Nonnull sexStr) {
            self.sexLimtStr = sexStr;
        }];
        return sexCell;
    }else if(indexPath.section == 4){
        QuDanCompleteTimeViewCell *timeCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanCompleteTimeViewCell" forIndexPath:indexPath];
        [timeCell setQuDanCompleteTimeViewCellAddTimeBlock:^{
           //添加时间
            [self actionSelectTime];
        }];
        return timeCell;
    }else{
        
        NSString *identifier = [self.cellIdentifyDic objectForKey:[NSString stringWithFormat:@"%@", indexPath]];
           if (identifier == nil) {
               identifier = [NSString stringWithFormat:@"NewTravelMediaCell%@", [NSString stringWithFormat:@"%@", indexPath]];
               [self.cellIdentifyDic setValue:identifier forKey:[NSString stringWithFormat:@"%@", indexPath]];
               // 注册Cell***************这里一定要注意一个问题，那就是你的cell 有没有用xib创建
               // 如果你是代码写的cell那么注册用下面的方法
               [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([QuDanCompleteTimeDetailViewCell class]) bundle:nil] forCellWithReuseIdentifier:identifier];
           }

        QuDanCompleteTimeDetailViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        cell.typeLab.text = self.timeDetailArr[indexPath.row];
        return cell;
    }
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsZero;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake(QD_IPHONE_WIDTH ,70);
    }else if(indexPath.section == 5){
        return CGSizeMake(QD_IPHONE_WIDTH ,55);
    }else{
        return CGSizeMake(QD_IPHONE_WIDTH ,65);
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        //选择发单技能
        [self setupSelectSkillData];
    }else if(indexPath.section == 5){
        QuDanCompleteTimeDetailViewCell *detailCell  = (QuDanCompleteTimeDetailViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        DLPickerView *pickerView = [[DLPickerView alloc] initWithDataSource:@[self.leftArr,self.leftArr]
                                withSelectedItem:[detailCell.timeLab.text componentsSeparatedByString:@"-"]
                            withSelectedBlock:^(id selectedItem) {
                detailCell.timeLab.text = [selectedItem componentsJoinedByString:@"-"];
            }
        ];
        pickerView.shouldDismissWhenClickShadow = YES;
        [pickerView show];
    }
}
/// 选择时间
-(void)actionSelectTime
{
    [BRStringPickerView showStringPickerWithTitle:@"请选择接单时间" dataSource:self.weekListArr defaultSelValue:self.weekListArr[0] isAutoSelect:NO resultBlock:^(id selectValue) {
        NSLog(@"selectValue:%@",selectValue);
        [self.timeDetailArr addObject:selectValue];
        [self.completeCollectionView reloadData];
    }];
}
/// 选择技能
-(void)setupSelectSkillData
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [[QuDanSendCenterNetWork shareInstance]requestSendSelectSkillNetWork:param viewController:self andBlock:^(id  _Nullable data, BOOL result) {
        NSLog(@"选择发布技能.data:%@",data);
        [self.skillListArr removeAllObjects];
        [self.skillNameArr removeAllObjects];
        self.skillListArr = [QuDanCategoryModel mj_objectArrayWithKeyValuesArray:data[@"category"]];
        for (QuDanCategoryModel *model in self.skillListArr) {
            NSLog(@"category.name:%@",model.caption);
            [self.skillNameArr addObject:model.caption];
        }
        [self actionShowSkillName];
    }];
}
/// 展示名称
-(void)actionShowSkillName{
    [BRStringPickerView showStringPickerWithTitle:@"请选择发单技能" dataSource:self.skillNameArr defaultSelValue:self.skillNameArr[0] isAutoSelect:NO resultBlock:^(id selectValue) {
        NSLog(@"selectValue:%@",selectValue);
        self.skillCell.skillTF.text = selectValue;
    }];
}
@end
